package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.team_circle.TeamCircleMessageAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.TeamCircleMessageModel;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/7/20.
 */

public class TeamCircleMessageActivity extends SlidingActivity implements View.OnClickListener, ResultSubscriber.OnResultListener, RecycleVAdapter.OnItemClickListener {

	private String companyId;
	private int companyType;
	private List<TeamCircleMessageModel> messageModels;
	private TeamCircleMessageAdapter adapter;

	@ViewInject(R.id.list_rv)
	private RecyclerView contentRv;
	@ViewInject(R.id.read_more_btn)
	private TextView readMoreBtn;

	@Override
	protected int getResourceId() {
		return R.layout.activity_team_circle_message;
	}

	@Override
	protected void init() {
		companyId = getIntent().getStringExtra("companyId");
		companyType = getIntent().getIntExtra("companyType", 1);
		messageModels = new ArrayList<>();
		adapter = new TeamCircleMessageAdapter(mContext, messageModels);

		topName.setText("消息");
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		rightIcon.setVisibility(View.GONE);
		leftIcon.setOnClickListener(this);

		contentRv.setHasFixedSize(true);
		contentRv.addItemDecoration(new JpDiver(mContext));
		contentRv.setLayoutManager(new LinearLayoutManager(mContext){
			@Override
			public boolean canScrollVertically() {
				return false;
			}
		});
		contentRv.setAdapter(adapter);
		readMoreBtn.setOnClickListener(this);
		adapter.addItemClickListener(this);

		getData();
	}

	@Override
	public void onClick(View v) {
		if(v == leftIcon){
			onBackPressed();
		} else if(v == readMoreBtn){
			getData();
		}
	}

	private void getData(){
		JsonObject param = new JsonObject();
		param.addProperty("companyId", companyId);;
		param.addProperty("companyType", companyType);;
		param.addProperty("mode", messageModels.size() == 0 ? 0 : 1);
		param.addProperty("time", messageModels.size() == 0 ? 0 : messageModels.get(messageModels.size()-1).getCreateTime());
		HttpHelper.getInstance(mContext).interactList(param, 0x01, this);
	}

	@Override
	public void onStart(int requestType) {}
	@Override
	public void onCompleted(int requestType) {}
	@Override
	public void onError(int requestType) {}
	@Override
	public void onNext(IModel data, int requestType) {
		if(data.getErrcode() == 0){
			List<TeamCircleMessageModel> teamCircleMessageModels = data.getList(TeamCircleMessageModel.class);
			if(teamCircleMessageModels != null && teamCircleMessageModels.size() > 0){
				messageModels.addAll(teamCircleMessageModels);
				adapter.updateAdapter(messageModels);
			} else{
				readMoreBtn.setText("无更多消息");
				readMoreBtn.setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
				readMoreBtn.setClickable(false);
			}
		}
	}

	@Override
	public void itemSelect(int position) {
		startActivity(new Intent(mContext, TeamCircleShowDetailActivity.class)
				.putExtra("id", messageModels.get(position).getScoopId()));
	}
}
